﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmKTVGoods : BaseForm
    {
        public FrmKTVGoods()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.cmbGoodsType.SelectedItem as CListItem;
            if (item != null)
            {
                SearchCondition condition = new SearchCondition();
                condition.AddCondition("GoodsType", item.Value, SqlOperator.Equal, true);

                string sql = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
                this.dgGoods.DataSource = BLLFactory<KTVGoods>.Instance.FindToDataTable(sql);
            }
        }

        private void BindData()
        {
            this.dgGoods.AutoGenerateColumns = false;
            this.dgType.AutoGenerateColumns = false;

            List<KTVGoodsTypeInfo> list = BLLFactory<KTVGoodsType>.Instance.GetAll();
            this.cmbGoodsType.Items.Clear();
            foreach (KTVGoodsTypeInfo typeInfo in list)
            {
                CListItem item = new CListItem(typeInfo.GoodsType, typeInfo.GoodsType);
                this.cmbGoodsType.Items.Add(item);
            }
            this.cmbGoodsType.Items.Insert(0, new CListItem("所有类别", ""));
            this.cmbGoodsType.SelectedIndex = 0;

            #region 列表绑定
            this.dgType.DataSource = BLLFactory<KTVGoodsType>.Instance.GetAllToDataTable();
            this.dgType.Columns[0].DataPropertyName = "ID";
            this.dgType.Columns[1].DataPropertyName = "TypeNo";
            this.dgType.Columns[2].DataPropertyName = "GoodsType";
            this.dgType.Columns[3].DataPropertyName = "IsNeedService";
            this.dgType.Columns[4].DataPropertyName = "Note";

            this.dgGoods.DataSource = BLLFactory<KTVGoods>.Instance.GetAllToDataTable();
            this.dgGoods.Columns[0].DataPropertyName = "ID";
            this.dgGoods.Columns[1].DataPropertyName = "ItemNo";
            this.dgGoods.Columns[2].DataPropertyName = "Name";
            this.dgGoods.Columns[3].DataPropertyName = "Price";
            this.dgGoods.Columns[4].DataPropertyName = "GoodsType";
            this.dgGoods.Columns[5].DataPropertyName = "Flag";
            this.dgGoods.Columns[6].DataPropertyName = "UnitName"; 
            this.dgGoods.Columns[7].DataPropertyName = "UnitCost";
            this.dgGoods.Columns[8].DataPropertyName = "StockQuantity";
            this.dgGoods.Columns[9].DataPropertyName = "AlarmQuantity"; 
            this.dgGoods.Columns[10].DataPropertyName = "Note"; 
            #endregion
        }

        private void FrmRoom_Load(object sender, EventArgs e)
        {
            BindData();
        }

        #region 按钮操作
        private void btnAddType_Click(object sender, EventArgs e)
        {
            FrmEditKTVGoodsType dlg = new FrmEditKTVGoodsType();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void btnEditType_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                FrmEditKTVGoodsType dlg = new FrmEditKTVGoodsType();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnDeleteType_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                BLLFactory<KTVGoodsType>.Instance.Delete(row.Cells[0].Value.ToString());
                BindData();
            }
        }

        private void btnDiscount_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgGoods.SelectedRows)
            {
                FrmKTVGoodsDiscount dlg = new FrmKTVGoodsDiscount();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnAddOne_Click(object sender, EventArgs e)
        {
            FrmEditKTVGoods dlg = new FrmEditKTVGoods();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void btnEditGoods_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgGoods.SelectedRows)
            {
                FrmEditKTVGoods dlg = new FrmEditKTVGoods();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnDeleteGoods_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            foreach (DataGridViewRow row in this.dgGoods.SelectedRows)
            {
                BLLFactory<KTVGoods>.Instance.Delete(row.Cells[0].Value.ToString());
            }
            BindData();
        } 
        #endregion

        #region 菜单操作
        private void menu_Type_AddType_Click(object sender, EventArgs e)
        {
            btnAddType_Click(null, null);
        }

        private void menu_Type_Edit_Click(object sender, EventArgs e)
        {
            btnEditType_Click(null, null);
        }

        private void menu_Type_Delete_Click(object sender, EventArgs e)
        {
            btnDeleteType_Click(null, null);
        }

        private void menu_Goods_AddOne_Click(object sender, EventArgs e)
        {
            btnAddOne_Click(null, null);
        }

        private void menu_Goods_Delete_Click(object sender, EventArgs e)
        {
            btnDeleteGoods_Click(null, null);
        }

        private void menu_Type_Update_Click(object sender, EventArgs e)
        {
            BindData();
        }


        private void menu_Goods_Discount_Click(object sender, EventArgs e)
        {
            btnDiscount_Click(null, null);
        }

        private void menu_Goods_Update_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #endregion

        #region DataGridView双击操作
        private void dgType_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEditType_Click(null, null);
        }

        private void dgGoods_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEditGoods_Click(null, null);
        } 
        #endregion

        private void txtPYM_TextChanged(object sender, EventArgs e)
        {
            List<KTVGoodsInfo> goodsList = new List<KTVGoodsInfo>();
            if (this.txtPYM.Text.Length > 0)
            {
                goodsList = BLLFactory<KTVGoods>.Instance.FindByName(this.txtPYM.Text);
            }
            else
            {
                goodsList = BLLFactory<KTVGoods>.Instance.GetAll();
            }
            this.dgGoods.DataSource = goodsList;
        }

    }
}
